home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / defrasterPort / defx.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  288 lines

  1. /*
  2.  * Copyright (c) 1994, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /* 
  23.  * Example to convert (and render) a font defined by defrasterfont()
  24.  * in irisGL to a X bitmap
  25.  * 
  26.  * Yusuf Attarwala 
  27.  *
  28.  * cc defx.c -o defx -lX11
  29.  *
  30.  */
  31.  
  32. #include <X11/Xlib.h>
  33. #include <X11/Xutil.h>
  34.  
  35. /* test_raster is compatible to defraster font */
  36.  
  37. int test_width  = 33;
  38. int test_height = 33;
  39.  
  40. unsigned short      test_raster[] = {
  41.         0x000f, 0xf000, 0x0000,
  42.         0x0010, 0x0800, 0x0000,
  43.         0x0060, 0x0600, 0x0000,
  44.         0x0180, 0x0180, 0x0000,
  45.         0x0600, 0x0060, 0x0000,
  46.         0x0800, 0x0010, 0x0000,
  47.         0x0803, 0xc010, 0x0000,
  48.         0x100f, 0xf008, 0x0000,
  49.         0x1038, 0x1c08, 0x0000,
  50.         0x2060, 0x0604, 0x0000,
  51.         0x2080, 0x0104, 0x0000,
  52.         0x4000, 0x0002, 0x0000,
  53.         0x8000, 0x0001, 0x0000,
  54.         0x8003, 0xc001, 0x0000,
  55.         0x8003, 0xc001, 0x0000,
  56.         0x8001, 0x8001, 0x0000,
  57.         0x8000, 0x0001, 0x0000,
  58.         0x8000, 0x0001, 0x0000,
  59.         0x8070, 0x0e01, 0x0000,
  60.         0x8070, 0x0e01, 0x0000,
  61.         0x8070, 0x0e01, 0x0000,
  62.         0x4070, 0x0e02, 0x0000,
  63.         0x2000, 0x0004, 0x0000,
  64.         0x2000, 0x0004, 0x0000,
  65.         0x1000, 0x0008, 0x0000,
  66.         0x1000, 0x0008, 0x0000,
  67.         0x0800, 0x0010, 0x0000,
  68.         0x0800, 0x0010, 0x0000,
  69.         0x0600, 0x0060, 0x0000,
  70.         0x01ff, 0xff80, 0x0000,
  71.         0x007f, 0xfe00, 0x0000,
  72.         0x001f, 0xf800, 0x0000,
  73.         0x000f, 0xf000, 0x0000};
  74.  
  75. unsigned char *defToX();
  76. unsigned char flipBit(),mirror();
  77.  
  78. #define BITUNIT 8
  79.  
  80. int width,height;
  81. Display *display; 
  82. int screen;
  83. Window win;                   
  84. XVisualInfo *vi;
  85. GC gc;
  86. Pixmap stip;
  87.  
  88. /* for testing
  89. static  char tbm[] = {0x7c, 
  90.               0xc3,
  91.               0xc0,
  92.                       0xc0,
  93.               0xfe,
  94.               0xc3,
  95.                       0xc3,
  96.               0xc3,
  97.               0x7e,
  98.               0x00};
  99. */
  100.  
  101. void
  102. testBitmap(xbm)
  103. unsigned char *xbm;
  104. {
  105.  
  106.     XEvent report;
  107.  
  108.     display = XOpenDisplay (NULL);
  109.     screen = DefaultScreen(display);
  110.     win = XCreateSimpleWindow (display, RootWindow(display,screen),
  111.                          0, 0, 200, 200,
  112.                          2, BlackPixel(display,screen),
  113.              WhitePixel(display,screen));
  114.     XMapRaised(display,win);
  115.     XSelectInput (display, win, ExposureMask);
  116.  
  117.     gc = XCreateGC(display,win,0,0);
  118.     XSetForeground (display, gc,BlackPixel(display,screen));
  119.     XSetBackground (display, gc,WhitePixel(display,screen));
  120.  
  121.     stip = XCreateBitmapFromData(display,RootWindow(display,screen),
  122.                  xbm,width,height);
  123.  
  124.     /* event loop */
  125.     for (;;)
  126.     {
  127.         XNextEvent (display, &report);
  128.         switch (report.type)
  129.         {
  130.             case Expose:
  131.                 drawIt();
  132.                 break;
  133.         }
  134.     }
  135.  
  136. }
  137.  
  138. drawIt()
  139. {
  140.     unsigned long planemask = 1;
  141.  
  142.     XClearWindow(display,win);
  143.     XCopyPlane(display,stip,win,gc,0,0,width,height,20,20,planemask);
  144.     XFlush(display);
  145. }
  146.  
  147. void
  148. main()
  149. {
  150.     unsigned char *xbm;
  151.  
  152.     xbm = defToX(test_width,test_height,test_raster);
  153.  
  154.     testBitmap(xbm);
  155.  
  156. }
  157.  
  158. unsigned char *
  159. defToX(w,h,r)
  160. int w,h;
  161. unsigned short *r;
  162. {
  163.     unsigned char *xbm,*uc;
  164.     unsigned short *us;
  165.  
  166.     int i,j;
  167.     int wide;
  168.     int tokensPerRow;
  169.     int outTokensPerRow;
  170.  
  171.     width   = w;
  172.     height  = h;
  173.  
  174.     if (width <= 16) {
  175.         tokensPerRow = 1;
  176.     }
  177.     else {
  178.     if (width%16 == 0) {
  179.         tokensPerRow = (int)(width / 16);
  180.     }
  181.     else {
  182.         tokensPerRow = (int)(width / 16) + 1;
  183.     }
  184.     }
  185.  
  186.     if (width <= BITUNIT) {
  187.     outTokensPerRow = 1;
  188.     }
  189.     else {
  190.     if (width%BITUNIT == 0) {
  191.         outTokensPerRow = (int)(width / BITUNIT);
  192.     }
  193.     else {
  194.         outTokensPerRow = (int)(width / BITUNIT) + 1;
  195.     }
  196.     }
  197.  
  198.     xbm = (unsigned char *)malloc(BITUNIT * outTokensPerRow * height);
  199.  
  200.     us = r;
  201.     for (i=height-1;i>=0;i--) {            /* top bottom */
  202.         uc = xbm + i*outTokensPerRow;
  203.         wide = 0;
  204.         for (j=0;j<tokensPerRow;j++) {
  205.  
  206.         *uc  = (unsigned char)(*us >> 8);
  207.         *uc = flipBit(*uc);
  208.         uc++;
  209.         wide += 8;
  210.  
  211.         if (wide < width) {
  212.             *uc  = (unsigned char)(*us);
  213.             *uc = flipBit(*uc);
  214.             uc++;
  215.             wide += 8;
  216.         }
  217.  
  218.         us++;
  219.         }
  220.     }
  221.  
  222.     return(xbm);
  223. }
  224.  
  225.  
  226. unsigned char
  227. flipBit(uc) 
  228. {
  229.     unsigned char left,right,ouc;
  230.     static unsigned char rightmask = 0x0f;
  231.     static unsigned char leftmask  = 0xf0;
  232.  
  233.     left  = (uc & leftmask)  >> 4;
  234.     right = (uc & rightmask) << 4;
  235.  
  236.     ouc = mirror(right) | mirror(left);
  237.  
  238.     return(ouc);
  239. }
  240.  
  241.  
  242. unsigned char 
  243. mirror(uc)
  244. {
  245.     unsigned char ret;
  246.     static unsigned char mirlut[241];
  247.     static int firstTime = 1;
  248.  
  249.     if (firstTime) {
  250.     firstTime = 0;
  251.  
  252.     mirlut[0]   = 0;
  253.     mirlut[1]   = 8;
  254.     mirlut[2]   = 4;
  255.     mirlut[3]   = 12;
  256.     mirlut[4]   = 2;
  257.     mirlut[5]   = 10;
  258.     mirlut[6]   = 6;
  259.     mirlut[7]   = 14;
  260.     mirlut[8]   = 1;
  261.     mirlut[9]   = 9;
  262.     mirlut[10]  = 5;
  263.     mirlut[11]  = 13;
  264.     mirlut[12]  = 3;
  265.     mirlut[13]  = 11;
  266.     mirlut[14]  = 7;
  267.     mirlut[15]  = 15;
  268.     mirlut[16]  = 128;
  269.     mirlut[32]  = 64;
  270.     mirlut[48]  = 192;
  271.     mirlut[64]  = 32;
  272.     mirlut[80]  = 160;
  273.     mirlut[96]  = 96;
  274.     mirlut[112] = 224;
  275.     mirlut[128] = 16;
  276.     mirlut[144] = 144;
  277.     mirlut[160] = 80;
  278.     mirlut[176] = 208;
  279.     mirlut[192] = 48;
  280.     mirlut[208] = 176;
  281.     mirlut[224] = 112;
  282.     mirlut[240] = 240;
  283.     }
  284.  
  285.     return(mirlut[uc]);
  286. }
  287.  
  288.